package com.ruochen.recipe.ui.activity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.blankj.utilcode.util.SizeUtils;
import com.ruochen.common.base.BaseActivity;
import com.ruochen.recipe.R;
import com.ruochen.recipe.databinding.ActivityMainBinding;
import com.ruochen.recipe.entity.MenuBean;
import com.ruochen.recipe.ui.fragment.account.AccountReconciliateFragment;
import com.ruochen.recipe.ui.fragment.order.OrderFragment;
import com.ruochen.recipe.ui.fragment.personal.PersonalFragment;
import com.ruochen.recipe.ui.fragment.raw.RawMaterialFragment;
import com.ruochen.recipe.widget.CustomerTabView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import q.rorbin.badgeview.Badge;
import q.rorbin.verticaltablayout.VerticalTabLayout;
import q.rorbin.verticaltablayout.adapter.TabAdapter;
import q.rorbin.verticaltablayout.widget.QTabView;
import q.rorbin.verticaltablayout.widget.TabView;

public class MainActivity extends BaseActivity<ActivityMainBinding> {

    private List<Fragment> fragments;
    private int currentIndex;

    @Override
    protected void initView(Bundle var1) {
        fragments = new ArrayList<>();

        TabMenuAdapter tabMenuAdapter = new TabMenuAdapter();
        for (int i = 0; i < tabMenuAdapter.getCount(); i++) {
            viewBinding.vtlTabMenu.addTab(new CustomerTabView(mContext).setIcon(tabMenuAdapter.getIcon(i))
                    .setTitle(tabMenuAdapter.getTitle(i)).setBadge(tabMenuAdapter.getBadge(i))
                    .setBackground(tabMenuAdapter.getBackground(i)));
        }

        fragments.add(new RawMaterialFragment());
        fragments.add(new RawMaterialFragment());
        fragments.add(new AccountReconciliateFragment());
        fragments.add(new OrderFragment());
        fragments.add(new PersonalFragment());
        viewBinding.vtlTabMenu.addOnTabSelectedListener(new VerticalTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabView tab, int position) {
                FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                fragmentTransaction.hide(fragments.get(currentIndex));
                if(!fragments.get(position).isAdded()){
                    fragmentTransaction.add(R.id.flContent, fragments.get(position));
                }
                fragmentTransaction.show(fragments.get(position)).commitAllowingStateLoss();
                currentIndex = position;
            }

            @Override
            public void onTabReselected(TabView tab, int position) {

            }
        });
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if(!fragments.get(currentIndex).isAdded()){
            fragmentTransaction.add(R.id.flContent, fragments.get(currentIndex));
        }
        fragmentTransaction.show(fragments.get(currentIndex)).commitAllowingStateLoss();
    }

    private class TabMenuAdapter implements TabAdapter {
        List<MenuBean> menus;

        TabMenuAdapter() {
            menus = new ArrayList<>();
            Collections.addAll(menus
                    , new MenuBean(R.mipmap.ic_menu_cpxd, R.mipmap.ic_menu_cpxd, "菜谱下单")
                    , new MenuBean(R.mipmap.ic_menu_yclxz, R.mipmap.ic_menu_yclxz, "原材料下单")
                    , new MenuBean(R.mipmap.ic_menu_dzzx, R.mipmap.ic_menu_dzzx, "对账中心")
                    , new MenuBean(R.mipmap.ic_menu_wddd, R.mipmap.ic_menu_wddd, "我的订单")
                    , new MenuBean(R.mipmap.ic_menu_grzx, R.mipmap.ic_menu_grzx, "个人中心"));
        }


        @Override
        public int getCount() {
            return menus.size();
        }

        @Override
        public TabView.TabBadge getBadge(int position) {
            return new TabView.TabBadge.Builder().setBadgeNumber(0).setBackgroundColor(ContextCompat.getColor(mContext, R.color.colorRed))
                    .setOnDragStateChangedListener(new Badge.OnDragStateChangedListener() {
                        @Override
                        public void onDragStateChanged(int dragState, Badge badge, View targetView) {

                        }
                    }).build();
        }

        @Override
        public QTabView.TabIcon getIcon(int position) {
            MenuBean menu = menus.get(position);
            return new TabView.TabIcon.Builder()
                    .setIcon(menu.mSelectIcon, menu.mNormalIcon)
                    .setIconGravity(Gravity.TOP)
                    .setIconMargin(0)
                    .setIconSize(24, 24)
                    .build();
        }

        @Override
        public QTabView.TabTitle getTitle(int position) {
            MenuBean menu = menus.get(position);
            return new TabView.TabTitle.Builder()
                    .setContent(menu.mTitle)
                    .setTextSize(14)
                    .setTextColor(ContextCompat.getColor(mContext, R.color.colorWhite),
                            ContextCompat.getColor(mContext, R.color.colorWhite))
                    .build();
        }

        @Override
        public int getBackground(int position) {
            return -1;
        }
    }
}